package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_PressureMonitor_DeviceParamServiceHelper;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.model.pressureMonitor_deviceParam.PressureMonitor_DeviceParamPutModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 压力监测仪 参数查询
 * @author 48310
 *
 */
@Component
public class MessageProcessor51 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	CachedCommandUtil cachedCommandUtil;
	@Autowired
	Device_PressureMonitor_DeviceParamServiceHelper device_pressureMonitor_deviceParamServiceHelper;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String ip = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_IP));
		String port = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.PORT));
		Double alarmMax = HzhrTypeUtil.object2double(parsedMessageObj.get(MessageFieldKeyEnum.RESP_OVER_PRESSURE_ALARM_VALUE)) * 0.01;
		Double alarmMin = HzhrTypeUtil.object2double(parsedMessageObj.get(MessageFieldKeyEnum.RESP_UNDER_PRESSURE_ALARM_VALUE)) * 0.01;
		Float collectFrequency = HzhrTypeUtil.object2float(parsedMessageObj.get(MessageFieldKeyEnum.RESP_NORMAL_COLLECT_FREQUENCY)) / 12;
		Float exceptionCollectFrequency = HzhrTypeUtil.object2float(parsedMessageObj.get(MessageFieldKeyEnum.RESP_EXCEPTION_COLLECT_FREQUENCY)) / 60;
		Float reportFrequency = HzhrTypeUtil.object2float(parsedMessageObj.get(MessageFieldKeyEnum.RESP_NORMAL_REPORT_PERIOD));
		Float exceptionReportFrequency = HzhrTypeUtil.object2float(parsedMessageObj.get(MessageFieldKeyEnum.RESP_EXCEPTION_REPORT_PERIOD)) / 60;
		String wakeTime = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_WAKE_TIME));

		IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByIotPlatformDeviceId(deviceId);
		if (iotDeviceModel == null) return null;
		String iotDeviceCode = iotDeviceModel.getCode();

		String messageId = parsedMessageObj.get(MessageFieldKeyEnum.MESSAGE_ID).toString();

		//更新设备参数
		Integer iotDeviceId = iotDeviceModel.getId();
		PressureMonitor_DeviceParamPutModel model = HzhrJsonUtil.convertObjectType(iotDeviceModel, PressureMonitor_DeviceParamPutModel.class);
		model.setIotPlatformIp(ip);
		model.setIotPlatformPort(port);
		model.setAlarmMax(alarmMax);
		model.setAlarmMin(alarmMin);
		model.setCollectFrequency(collectFrequency);
		model.setExceptionCollectFrequency(exceptionCollectFrequency);
		model.setReportFrequency(reportFrequency);
		model.setExceptionReportFrequency(exceptionReportFrequency);
		model.setReportTime(wakeTime);
		device_pressureMonitor_deviceParamServiceHelper.putByPressureMonitorId(iotDeviceId, model);

		//清除指令缓存
		cachedCommandUtil.deleteCachedCommand(deviceId, iotDeviceCode, messageId);

		return null;
	}
}
